/**
 * 系统模块
 */
layui.define(function (exports) {
    var system = {
        iframeLayer: function (options) {
            // 来源窗口，调用的时候直接把window传进去，方便在子页面中操作调用页，因为如果有多层嵌套查找调用页就不容易了
            var sourceWindow = options.sourceWindow || window;
            // 目标窗口
            var targetName = options.targetName || 'self';
            // 弹窗回调
            var success = options.success;
            var yes = options.yes;

            delete options.success;
            delete options.yes;
            delete options.sourceWindow;
            delete options.targetName;
            // TODO 根据不同的屏幕尺寸设置不同的宽度
            var config = {
                type: 2,
                title: '窗口',
                content: '',
                maxmin: true,
                area: ['80%'],
                maxHeight: '80%',
                success: function (layerWindow, layerIndex) {
                    var iframe = layerWindow.find('iframe')[0];
                    // 取得目标窗口高度
                    var windowHeight = window[targetName].layui.jquery(window).height();
                    // 取得iframe高度
                    var iframeHeight = layui.jquery(iframe).contents().find('html').height();
                    // 弹层高度
                    var layerHeight = this.maxHeight || windowHeight;
                    if (/^\d+%$/.test(layerHeight)) {
                        layerHeight = windowHeight * parseFloat(layerHeight) / 100;
                    }
                    // 标题高度
                    var titleHeight = layerWindow.find('.layui-layer-title').outerHeight() || 0;
                    // 按钮高度
                    var buttonHeight = layerWindow.find('.layui-layer-btn').outerHeight() || 0;
                    // 内容高度
                    var contentHeight = layerHeight - titleHeight - buttonHeight;
                    // 计算iframe高度
                    iframeHeight = iframeHeight > contentHeight ? contentHeight : iframeHeight;
                    // 计算层高度
                    layerHeight = iframeHeight > contentHeight ? layerHeight : iframeHeight + titleHeight + buttonHeight;
                    // 计算定位
                    var topPosition = (windowHeight - layerHeight) * 0.5;
                    // 设置窗口高度
                    layerWindow.css({height: layerHeight, top: topPosition});
                    // 设置iframe高度
                    layerWindow.find('iframe').css({height: iframeHeight});
                    // 注入来源对象
                    iframe.contentWindow.SOURCE_WINDOW = sourceWindow;
                    iframe.contentWindow.TARGET_NAME = targetName;
                    iframe.contentWindow.LAYER_INDEX = layerIndex;
                    typeof success === 'function' && success.apply(this, arguments);
                },
                yes: function (layerIndex, layerWindow) {
                    window[targetName].layui.layer.close(layerIndex);
                    typeof yes === 'function' && yes.apply(this, arguments);
                }
            };
            return window[targetName].layui.layer.open(layui.jquery.extend(config, options));
        },
        /**
         * 打开页面
         */
        openPage: function (link, target, name) {
            if (!link) {
                return null;
            }
            target = target || 'tabs';
            if (target == 'tabs') {
                top.layui.index.openTabsPage(link, '支出明细');
                return null;
            }
        },
    };
    exports('system', system);
});